﻿using NPOI.SS.Formula.Functions;
using OSSUtility.Device;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Ozone.Device
{
    //润泽注射泵
    public class DevSY09S : DevBase
    {
        //COM
        public DevSY09S(int CommType, string FSrvIP, int FPort) : base(CommType, FSrvIP, FPort)
        {

        }
        /// <summary>
        /// 注射泵复位
        /// </summary>
        /// <returns></returns>
        public bool PumpReset()
        {
            //CC 00 44 01 00 DD EE 01
            byte[] bytesData = new byte[] { 0xCC, 0x00, 0x45, 0x00, 0x00, 0xDD };
            // 计算字节数组的和
            int sum = 0;
            foreach (byte b in bytesData)
            {
                sum += b;
            }
            // 提取低8位和高8位
            byte lowByte = (byte)(sum & 0xFF);
            byte highByte = (byte)((sum >> 8) & 0xFF);
            // 拼接结果（低位在前，高位在后）
            byte[] result = new byte[] { lowByte, highByte };
            // 方法1: 使用Concat (需要LINQ)
            byte[] combined1 = bytesData.Concat(result).ToArray();
            byte[] RecvData = session_.sendAndReadByte(combined1);
            if (RecvData == null)
                return false;
            if (RecvData.Length != 8)
                return false;
            return true;
        }
        /// <summary>
        /// 注射泵打液
        /// </summary>
        /// <param name="PumpML">打液体积</param>
        /// <returns></returns>

        public bool PumpRun(int PumpML)
        {
            if (PumpML > 8)
            {
                int WhilePuml = PumpML / 8;
                int EndPumpML = PumpML % 8;
                for (int i = 0; i < WhilePuml; i++)
                {
                    PumpRun3ML(8);
                    PumpRun3ML(0);
                }
                PumpRun3ML(EndPumpML);
                PumpRun3ML(0);
            }
            else
            {
                PumpRun3ML(PumpML);
                PumpRun3ML(0);
            }
            return true;
        }
        /// <summary>
        /// 注射泵吸液/打液
        /// </summary>
        /// <param name="PumpML">吸液/打液体积</param>
        /// <returns></returns>
        public bool PumpRun3ML(int PumpML)
        {
            //切阀
            if (PumpML == 0)
            {
                SwitchValve(1);
            }
            else
            {
                SwitchValve(2);
            }
            //PumpML = PumpML * 1200;
            PumpML = PumpML * 480;
            byte[] bytesDataFlow = BitConverter.GetBytes(PumpML);
            // bytesData = new byte[] { 0xCC, 0x00, 0x4E, 0x0E, 0x10, 0xDD };
            //CC 00 44 01 00 DD EE 01
            byte[] bytesData = new byte[] { 0xCC, 0x00, 0x4E, 0x00, 0x00, 0xDD };
            bytesData[3] = bytesDataFlow[0];
            bytesData[4] = bytesDataFlow[1];
            // 计算字节数组的和
            int sum = 0;
            foreach (byte b in bytesData)
            {
                sum += b;
            }
            // 提取低8位和高8位
            byte lowByte = (byte)(sum & 0xFF);
            byte highByte = (byte)((sum >> 8) & 0xFF);
            // 拼接结果（低位在前，高位在后）
            byte[] result = new byte[] { lowByte, highByte };
            // 方法1: 使用Concat (需要LINQ)
            byte[] combined1 = bytesData.Concat(result).ToArray();
            byte[] RecvData = session_.sendAndReadByte(combined1);
            if (RecvData == null)
                return false;
            if (RecvData.Length != 8)
                return false;
            int nTimes = 0;
            while (GetSY09MLLocation() != PumpML)
            {
                if (nTimes >= 60)
                    return false;
                nTimes++;
                Thread.Sleep(1000);
            }
            return true;
        }
        /// <summary>
        /// 切阀
        ///   1号切换 NC端口  出 （排液）
        ///   2号切换 NO端口  进 （吸液）
        /// </summary>
        /// <param name="nValveNO">1或2</param>
        /// <returns></returns>
        public bool SwitchValve(int nValveNO)
        {
            byte[] bytesData = new byte[] { 0xCC, 0x00, 0x60, 0x01, 0x00, 0xDD };
            if (nValveNO == 2)
            {
                bytesData[2] = 0X61;
            }
            // 计算字节数组的和
            int sum = 0;
            foreach (byte b in bytesData)
            {
                sum += b;
            }
            // 提取低8位和高8位
            byte lowByte = (byte)(sum & 0xFF);
            byte highByte = (byte)((sum >> 8) & 0xFF);
            // 拼接结果（低位在前，高位在后）
            byte[] result = new byte[] { lowByte, highByte };
            // 方法1: 使用Concat (需要LINQ)
            byte[] combined1 = bytesData.Concat(result).ToArray();
            byte[] RecvData = session_.sendAndReadByte(combined1);
            if (RecvData == null)
                return false;
            if (RecvData.Length != 8)
                return false;
            Thread.Sleep(3000);
            return true;
        }
        /// <summary>
        /// 获取注射泵绝对位置
        /// </summary>
        /// <returns></returns>
        public int GetSY09MLLocation()
        {

            byte[] bytesData = new byte[] { 0xCC, 0x00, 0x68, 0x00, 0x00, 0xDD };
            // 计算字节数组的和
            int sum = 0;
            foreach (byte b in bytesData)
            {
                sum += b;
            }
            // 提取低8位和高8位
            byte lowByte = (byte)(sum & 0xFF);
            byte highByte = (byte)((sum >> 8) & 0xFF);
            // 拼接结果（低位在前，高位在后）
            byte[] result = new byte[] { lowByte, highByte };
            // 方法1: 使用Concat (需要LINQ)
            byte[] combined1 = bytesData.Concat(result).ToArray();
            byte[] RecvData = session_.sendAndReadByte(combined1);
            if (RecvData == null)
                return -1;
            if (RecvData.Length != 8)
                return -1;
            byte[] targetBytes = new byte[2] { RecvData[3], RecvData[4] };
            int pumplength = BitConverter.ToUInt16(targetBytes, 0);
            return pumplength;
        }
    }
}
